{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1492e99a",
   "metadata": {},
   "source": [
    "# 百度AI开放平台  \n",
    " \n",
    "## 通用物体和场景识别  \n",
    "\n",
    "* 接口描述  \n",
    "\n",
    "> 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。  \n",
    "\n",
    "* 请求示例   \n",
    "\n",
    "> HTTP方法：POST  \n",
    "\n",
    "* 请求URL  \n",
    "\n",
    "> https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general  \n",
    "\n",
    "### 鉴权认证机制（access_token）\n",
    "* 获取Access Token\n",
    "* 获取access_token示例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77855026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9d8de300fb36a30749977f2698b80d6e.315360000.1964940835.282335-25904770', 'expires_in': 2592000, 'session_key': '9mzdCXJBx3YWkPXdXjOhWZJyv9zs3zjoIGkCUcOe0zMhVn7NrdTBmmmxhxM17NbKTWQBgg+X9nO47nDLHdbd19T3SIHIfw==', 'access_token': '24.2db40a237722dd2456a20c13920a63fa.2592000.1652172835.282335-25904770', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'b55149094a0a2a9b75f163dd02916dac'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'xfEaT2GGjBc8ExifRtZlI2RT',\n",
    "    'client_secret':'7yVlFC2uCGvs0lRfmkVewvXNCoooLGy3'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ba8a7fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.2db40a237722dd2456a20c13920a63fa.2592000.1652172835.282335-25904770'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token=response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70d56e5f",
   "metadata": {},
   "source": [
    "## 图像识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "162997e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result_num': 5,\n",
       " 'result': [{'keyword': '异国短毛猫', 'score': 0.375094, 'root': '动物-哺乳动物'},\n",
       "  {'keyword': '猫', 'score': 0.318675, 'root': '动物-猫'},\n",
       "  {'keyword': '玩具', 'score': 0.265763, 'root': '商品-玩具'},\n",
       "  {'keyword': '小狗', 'score': 0.149829, 'root': '动物-狗'},\n",
       "  {'keyword': '短毛家猫', 'score': 0.018329, 'root': '动物-哺乳动物'}],\n",
       " 'log_id': 1513077817934504776}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('cat.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1f59848",
   "metadata": {},
   "source": [
    " ## 菜品识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b490e67f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result_num': 1,\n",
       " 'result': [{'has_calorie': False,\n",
       "   'name': '非菜',\n",
       "   'probability': '0.577924',\n",
       "   'baike_info': {}}],\n",
       " 'log_id': 1513078245044757485}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('cai.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    'top_num':5\n",
    "    \n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9dbf34c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result_num': 5,\n",
       " 'result': [{'calorie': '26',\n",
       "   'has_calorie': True,\n",
       "   'name': '干锅花菜',\n",
       "   'probability': '0.868681',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%B9%B2%E9%94%85%E8%8A%B1%E8%8F%9C/9836545',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/4bed2e738bd4b31c87018ecd4d9f307f9e2f07085239',\n",
       "    'description': '干锅花菜是一道以花菜、秀珍菇、蘑菇、青红尖椒为主要食材，以豆瓣酱、豆豉酱、干辣椒段、花椒粒、姜片、蒜片、葱段、酱油、盐、鸡粉、白糖为辅料制作而成的美食，口味香辣。'}},\n",
       "  {'calorie': '26',\n",
       "   'has_calorie': True,\n",
       "   'name': '炒花菜',\n",
       "   'probability': '0.0908211',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%82%92%E8%8A%B1%E8%8F%9C/4330660',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/060828381f30e92425cfdd7f46086e061d95f711',\n",
       "    'description': '炒花菜是一道菜品，制作原料有花菜、大葱、盐等。烹饪简单，营养丰富，菜花比较难炒熟，所以可以在炒菜之前用水抄一下。'}},\n",
       "  {'calorie': '37',\n",
       "   'has_calorie': True,\n",
       "   'name': '干锅包菜',\n",
       "   'probability': '0.0222421',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%B9%B2%E9%94%85%E5%8C%85%E8%8F%9C/8974278',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/6609c93d70cf3bc746482095de00baa1cd112a7b',\n",
       "    'description': '干锅包菜是一道四川的地方传统名菜，属于川菜菜系，口感更嫩滑，入口绵软、入味好吃。主要原料是卷心菜、香菇、竹笋、猪肉等；工艺是炒，各个地区有不同的做法。'}},\n",
       "  {'calorie': '133',\n",
       "   'has_calorie': True,\n",
       "   'name': '干锅',\n",
       "   'probability': '0.00407002',\n",
       "   'baike_info': {}},\n",
       "  {'has_calorie': False,\n",
       "   'name': '干锅三素',\n",
       "   'probability': '0.0029231',\n",
       "   'baike_info': {}}],\n",
       " 'log_id': 1513078873427494981}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('caihua.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    'top_num':5\n",
    "    \n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91826d52",
   "metadata": {},
   "source": [
    "## 动物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "459fb98e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': [{'name': '蓝凤冠鸠',\n",
       "   'score': '0.519778',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%93%9D%E5%87%A4%E5%86%A0%E9%B8%A0/7529612',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/64380cd7912397dd68073b965582b2b7d0a28722',\n",
       "    'description': '蓝凤冠鸠(学名：Goura cristata)是鸠鸽类中最漂亮的成员，也是鸽鸠类中体型最大的一种，几乎和火鸡一般大。蓝凤冠鸠通体呈蓝色，羽毛光滑而整洁，在尾部和翅下分别有一块白色区域，翅膀中部部分至前胸略呈紫色，红色的眼睛炯炯有神，眼睛周围的羽毛颜色较深，腿部还长有小斑点。蓝凤冠鸠常栖息在近水的森林或沼泽地区，成小群徘徊觅食。习性如雉鸡一般。主食各种植物的种子，果实及昆虫等。分布于印度尼西亚各岛屿。(概述图片参考资料来源：)'}},\n",
       "  {'name': '维多凤冠鸠',\n",
       "   'score': '0.251918',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%BB%B4%E5%A4%9A%E5%87%A4%E5%86%A0%E9%B8%A0/4408262',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/9a1151c2d71cea08e5dd3b98',\n",
       "    'description': '维多凤冠鸠，Goura victoria (Fraser,1844)，脊索动物门、鸟纲、鸽形目、鸠鸽科、冠鸠属的一个物种，分布于太平洋诸岛屿，包括中国的台湾省、东沙群岛、西沙群岛、中沙群岛、南沙群岛以及菲律宾、文莱、马来西亚、新加坡、印度尼西亚的苏门答腊、爪哇岛以及巴布亚新几内亚。'}},\n",
       "  {'name': '罗岛蓝鸠',\n",
       "   'score': '0.129298',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%BD%97%E5%B2%9B%E8%93%9D%E9%B8%A0/2642069',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/c2cec3fdfc039245d68818026fc5b3c27d1ed21b8f26',\n",
       "    'description': '罗岛蓝鸠(学名：Alectroenas rodericana)是马斯克林群岛的罗德里格斯岛上已灭绝的鸽。对它们的了解只有一些胸骨及其他骨头的亚化石，并一些描述。它们的体型如白胸森鸠般大小，颜色呈灰色。它们相信是一种温驯的鸟类，并且喜欢吃瓜的种子。于1693年，在岛上仍可见它们的行踪，并在海岛上筑巢。'}},\n",
       "  {'name': '蓝冠鸽',\n",
       "   'score': '0.0541712',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%93%9D%E5%86%A0%E9%B8%BD/2159422',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/58c3acb7f8573fe730add12d',\n",
       "    'description': '习 性：成群结队栖息于森林地带。主食果实、种籽及昆虫。在离地6~7公尺的树上筑大型的鸽巢 ，每窝只产蛋1枚，约28~29天出雏，30~36天即可离巢飞翔。'}},\n",
       "  {'name': '蕉鹃',\n",
       "   'score': '0.00341302',\n",
       "   'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%95%89%E9%B9%83%E7%A7%91/2501193',\n",
       "    'image_url': 'https://bkimg.cdn.bcebos.com/pic/54fbb2fb43166d22a4f96670412309f79152d24c',\n",
       "    'description': '蕉鹃科(学名：Musophagidae)有6属23种。该科鸟类一般为中等体型，体长35~75厘米，体重230~950克。体色或者大致呈绿色，翼和尾绿色、蓝色或紫色；或者全身大部分为青紫色或灰色。通常有漂亮的羽冠和长尾巴。该鸟具半对趾足，第四趾能够随意向前、向后旋转。第二、三趾总指向前方。分布于非洲中南部。栖息于常青林和林谷，极少数栖息于大草原。有些山地品种则可能分布在非洲最高的几座山上。食物以果实为主，也吃某些无脊椎动物，比如昆虫(特别是在繁殖季节)。蕉鹃为树栖性鸟类，不善飞行，但在树冠上奔跑迅速。人们对蕉鹃一度知之甚少，以致一直称它为“食大蕉的鸟”(plantain-eaters)，即蕉鹃科“Musophagidae”一词的字面意思。不过，在了解了这种体型中等、尾长、翅短而圆的鸟极少或从来不食大蕉和香蕉后，便用 “turaco”一词取代了原来不当的叫法。事实上，在非洲，靠食大蕉生存下去根本不可能，因为人们将大蕉引入非洲大陆只是晚期的事。(概述图片来源)'}}],\n",
       " 'log_id': 1513079792274919055}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "动物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('bird.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    'top_num':5\n",
    "    \n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34195bd0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
